<?php
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$address = $_POST['address'];
$data_set = date_default_timezone_set('Asia/Shanghai');
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$date = date('H:i, jS F Y');
?>
<html>
    <head>
        <title>Bob's Auto Parts - Order Results</title>
    </head>
    <body>
        <h1>Bob's Auto Parts</h1>
        <h2>Order Results</h2>
       
        <?php
            echo $_SERVER['DOCUMENT_ROOT'];
            echo '<p>Order processed at '.$date.'</p>';
            
            $totalqty = $tireqty + $oilqty + $sparkqty;
            echo "Items ordered: ".$totalqty."<br/>";
            
            echo '<p>Your order is as follows:</p>';
            if ($totalqty == 0) {
                echo '<p style="color:red">';
                echo 'You did not order anything on the previous page!<br />';
                echo '</p>';
                exit();
            } else {
                if ($tireqty > 0) {
                    echo $tireqty. ' tires<br />';
                }
                if ($oilqty > 0) {
                    echo $oilqty. ' bottles of oil<br />';
                }
                if ($sparkqty > 0) {
                    echo $sparkqty. ' spark plugs<br />';
                }
            }
            
            define('TIREPRICE', 100);
            define('OILPRICE', 10);
            define('SPARKPRICE', 4);
            
            $totalamount = $tireqty * TIREPRICE 
                    + $oilqty * OILPRICE
                    + $sparkqty * SPARKPRICE;
            echo "Subtotal: $".number_format($totalamount, 2)."<br/>";
            
            $tax_rate = 0.10;
            $totalamountwithtax = $totalamount * (1 + $tax_rate);
            echo "Total including tax: $".number_format($totalamountwithtax, 2)."<br/>";
            #echo $totalamount;
            #$totalamount = 'Hello'; 
            #echo $totalamount;
            $outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
                    .$sparkqty." spark plugs\t\$".$totalamount
                    ."\t".$address."\n";
            
            $fp = @fopen("$DOCUMENT_ROOT/../orders/orders.txt", "ab");
            
            flock($fp, LOCK_EX);
            if (!$fp) {
                echo "<p><strong>Your order could not be processed at this time.
                Please try again later.</strong></p></body></html>";
                exit;
            }
            
            fwrite($fp, $outputstring, strlen($outputstring));
            flock($fp, LOCK_UN);
            fclose($fp);
            
            echo "<p>Order written.</p>";
        ?>
        <a href="orderform.html">New Order</a>
    </body>
</html>


